package br.com.light.adapter.retorno.consulta;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.XMLOutputter;

import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;

public class MappingRetornoConsultaSPtoRFC implements StreamTransformation{

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 * @throws StreamTransformationException 
	 */
	public static void main(String[] args) throws FileNotFoundException, StreamTransformationException {
		File file = new File("D:/documentos/Complex/Light/RetornoConsultaSP_5.xml");
		InputStream in = new FileInputStream(file);
		
		File fileOut = new File("D:/documentos/Complex/Light/RetornoConsultaRFC_5.xml");
		OutputStream out = new FileOutputStream(fileOut);
		
		new MappingRetornoConsultaSPtoRFC().execute(in, out);

	}

	public void execute(InputStream in, OutputStream out)
			throws StreamTransformationException {
		try {
			
			//Criando estrutura para SP
			Element header = new Element("ZNFSEF_STATUS_CHECK_IN_SP", "ns0", "urn:sap-com:document:sap:rfc:functions");
			
			Element it_alerta = new Element("IT_ALERTA");
			header.addContent(it_alerta);
			Element item_alerta = new Element("item");
			it_alerta.addContent(item_alerta);
			Element error_code_alerta = new Element("ERROR_CODE");
			item_alerta.addContent(error_code_alerta);
			Element message_alerta = new Element("MESSAGE");
			item_alerta.addContent(message_alerta);
			Element correction_alerta = new Element("CORRECTION");
			item_alerta.addContent(correction_alerta);		
			
			Element it_error = new Element("IT_ERROR");
			header.addContent(it_error);
			Element item_error = new Element("item");
			it_error.addContent(item_error);
			Element error_code = new Element("ERROR_CODE");
			item_error.addContent(error_code);
			Element message_error = new Element("MESSAGE");
			item_error.addContent(message_error);
			Element correction_error = new Element("CORRECTION");
			item_error.addContent(correction_error);
			
			Element it_nfse = new Element("IT_NFSE");
			header.addContent(it_nfse);
			Element item_nfse = new Element("item");
			it_nfse.addContent(item_nfse);
			Element mandt = new Element("MANDT");
			item_nfse.addContent(mandt);
			Element docnum = new Element("DOCNUM");
			item_nfse.addContent(docnum);
			Element nfsenum = new Element("NFSENUM");
			item_nfse.addContent(nfsenum);
			Element vercode = new Element("VERCODE");
			item_nfse.addContent(vercode);
			Element issdate = new Element("ISSDATE");
			item_nfse.addContent(issdate);
			Element isstime = new Element("ISSTIME");
			item_nfse.addContent(isstime);
			Element cancode = new Element("CANCODE");
			item_nfse.addContent(cancode);
			Element candate = new Element("CANDATE");
			item_nfse.addContent(candate);
			Element cantime = new Element("CANTIME");
			item_nfse.addContent(cantime);
			Element stat_out = new Element("STAT_OUT");
			item_nfse.addContent(stat_out);
			Element stat_in = new Element("STAT_IN");
			item_nfse.addContent(stat_in);
			Element shipnum = new Element("SHIPNUM");
			item_nfse.addContent(shipnum);
			Element rpsnum = new Element("RPSNUM");
			item_nfse.addContent(rpsnum);
			Element ship_prot = new Element("SHIP_PROT");
			item_nfse.addContent(ship_prot);
			Element docsub = new Element("DOCSUB");
			item_nfse.addContent(docsub);	
			Element docori = new Element("DOCORI");
			item_nfse.addContent(docori);
			Element cod_erro = new Element("COD_ERRO");
			item_nfse.addContent(cod_erro);	
			Element signature = new Element("SIGNATURE");
			item_nfse.addContent(signature);
			Element opsimples = new Element("OPSIMPLES");
			item_nfse.addContent(opsimples);
			Element numguia = new Element("NUMGUIA");
			item_nfse.addContent(numguia);
			Element dtquitguia = new Element("DTQUITGUIA");
			item_nfse.addContent(dtquitguia);
			Element amnt_credito = new Element("AMNT_CREDITO");
			item_nfse.addContent(amnt_credito);
			
			Element i_candate = new Element("I_CANDATE");
			header.addContent(i_candate);
			
			Element i_id_sist = new Element("I_ID_SIST");
			header.addContent(i_id_sist);
			i_id_sist.setText("ECC");
			
			Element i_nfse = new Element("I_NFSE");
			header.addContent(i_nfse);
			Element i_nfse_shipnum = new Element("SHIPNUM");
			i_nfse.addContent(i_nfse_shipnum);
			Element i_nfse_nfsenum = new Element("NFSENUM");
			i_nfse.addContent(i_nfse_nfsenum);
			Element i_nfse_issue_date = new Element("NFSE_ISSUE_DATE");
			i_nfse.addContent(i_nfse_issue_date);
			Element i_nfse_vercod = new Element("NFSE_VERCOD");
			i_nfse.addContent(i_nfse_vercod);
			
			Element i_rps = new Element("I_RPS");
			header.addContent(i_rps);
			Element i_rpsnum = new Element("RPSNUM");
			i_rps.addContent(i_rpsnum);
			Element i_rps_series = new Element("RPS_SERIES");
			i_rps.addContent(i_rps_series);
			Element i_rps_type = new Element("RPS_TYPE");
			i_rps.addContent(i_rps_type);
			Element i_rps_issue_date = new Element("ISSUE_DATE");
			i_rps.addContent(i_rps_issue_date);
			Element i_rps_nat_oper = new Element("NAT_OPER");
			i_rps.addContent(i_rps_nat_oper);
			Element i_rps_ret_code = new Element("RET_CODE");
			i_rps.addContent(i_rps_ret_code);
			Element i_rps_flag_simples = new Element ("FLAG_SIMPLES");
			i_rps.addContent(i_rps_flag_simples);
			Element i_rps_flag_cultural = new Element("FLAG_CULTURAL");
			i_rps.addContent(i_rps_flag_cultural);
			Element i_rps_status = new Element("RPS_STATUS");
			i_rps.addContent(i_rps_status);
			Element i_rpsnum_sub = new Element ("RPSNUM_SUB");
			i_rps.addContent(i_rpsnum_sub);
			Element i_rps_series_sub = new Element ("RPS_SERIES_SUB");
			i_rps.addContent(i_rps_series_sub);
			Element i_rps_type_sub = new Element ("RPS_TYPE_SUB");
			i_rps.addContent(i_rps_type_sub);
			Element i_rps_amnt_serv = new Element ("AMNT_SERV");
			i_rps.addContent(i_rps_amnt_serv);
			Element i_rps_amnt_dedu = new Element ("AMNT_DEDU");
			i_rps.addContent(i_rps_amnt_dedu);
			Element i_rps_amnt_pis = new Element ("AMNT_PIS");
			i_rps.addContent(i_rps_amnt_pis);
			Element i_rps_amnt_cof = new Element ("AMNT_COF");
			i_rps.addContent(i_rps_amnt_cof);
			Element i_rps_amnt_inss = new Element ("AMNT_INSS");
			i_rps.addContent(i_rps_amnt_inss);
			Element i_rps_amnt_ir = new Element ("AMNT_IR");
			i_rps.addContent(i_rps_amnt_ir);
			Element i_rps_amnt_csll = new Element ("AMNT_CSLL");
			i_rps.addContent(i_rps_amnt_csll);
			Element i_rps_flag_iss_ret = new Element ("FLAG_ISS_RET");
			i_rps.addContent(i_rps_flag_iss_ret);
			Element i_rps_amnt_iss = new Element ("AMNT_ISS");
			i_rps.addContent(i_rps_amnt_iss);
			Element i_rps_amnt_ret = new Element ("AMNT_ISS_RET");
			i_rps.addContent(i_rps_amnt_ret);
			Element i_rps_amnt_other_ret = new Element ("AMNT_OTHER_RET");
			i_rps.addContent(i_rps_amnt_other_ret);
			Element i_rps_amnt_base = new Element ("AMNT_BASE");
			i_rps.addContent(i_rps_amnt_base);
			Element i_rps_aliquot = new Element ("ALIQUOT");
			i_rps.addContent(i_rps_aliquot);
			Element i_rps_amnt_net = new Element ("AMNT_NET");
			i_rps.addContent(i_rps_amnt_net);
			Element i_rps_amnt_disc_uncond = new Element ("AMNT_DISC_UNCOND");
			i_rps.addContent(i_rps_amnt_disc_uncond);
			Element i_rps_amnt_cond = new Element ("AMNT_DISC_COND");
			i_rps.addContent(i_rps_amnt_cond);
			Element i_rps_serv_item = new Element ("SERV_ITEM");
			i_rps.addContent(i_rps_serv_item);
			Element i_rps_cnae = new Element ("CNAE");
			i_rps.addContent(i_rps_cnae);
			Element i_rps_mun_tax_code = new Element ("MUN_TAX_CODE");
			i_rps.addContent(i_rps_mun_tax_code);
			Element i_rps_text = new Element ("TEXT");
			i_rps.addContent(i_rps_text);
			Element i_rps_mun_code = new Element("MUN_CODE");
			i_rps.addContent(i_rps_mun_code);
			Element i_rps_prov_cnpj = new Element ("PROV_CNPJ");
			i_rps.addContent(i_rps_prov_cnpj);
			Element i_rps_prov_im = new Element ("PROV_IM");
			i_rps.addContent(i_rps_prov_im);
			Element i_rps_taker_cnpj = new Element ("TAKER_CNPJ");
			i_rps.addContent(i_rps_taker_cnpj);
			Element i_rps_taker_cpf = new Element ("TAKER_CPF");
			i_rps.addContent(i_rps_taker_cpf);
			Element i_rps_taker_im = new Element ("TAKER_IM");
			i_rps.addContent(i_rps_taker_im);
			Element i_rps_taker_name = new Element ("TAKER_NAME");
			i_rps.addContent(i_rps_taker_name);
			Element i_rps_taker_addr_street = new Element ("TAKER_ADDR_STREET");
			i_rps.addContent(i_rps_taker_addr_street);
			Element i_rps_taker_addr_num = new Element ("TAKER_ADDR_NUM");
			i_rps.addContent(i_rps_taker_addr_num);
			Element i_rps_taker_addr_comp = new Element ("TAKER_ADDR_COMP");
			i_rps.addContent(i_rps_taker_addr_comp);
			Element i_rps_taker_addr_dist = new Element ("TAKER_ADDR_DIST");
			i_rps.addContent(i_rps_taker_addr_dist);
			Element i_rps_taker_city = new Element ("TAKER_CITY");
			i_rps.addContent(i_rps_taker_city);
			Element i_rps_taker_city_code = new Element ("TAKER_CITY_CODE");
			i_rps.addContent(i_rps_taker_city_code);
			Element i_rps_taker_uf = new Element ("TAKER_UF");
			i_rps.addContent(i_rps_taker_uf);
			Element i_rps_taker_addr_post_code = new Element ("TAKER_ADDR_POST_CODE");
			i_rps.addContent(i_rps_taker_addr_post_code);
			Element i_rps_taker_email = new Element ("TAKER_EMAIL");
			i_rps.addContent(i_rps_taker_email);
			Element i_rps_taker_phone = new Element ("TAKER_PHONE");
			i_rps.addContent(i_rps_taker_phone);
			Element i_rps_brok_cpf = new Element ("BROK_CPF");
			i_rps.addContent(i_rps_brok_cpf);
			Element i_rps_brok_cnpj = new Element ("BROK_CNPJ");
			i_rps.addContent(i_rps_brok_cnpj);
			Element i_rps_brok_im = new Element ("BROK_IM");
			i_rps.addContent(i_rps_brok_im);
			Element i_rps_brok_name = new Element ("BROK_NAME");
			i_rps.addContent(i_rps_brok_name);
			Element i_rps_work_code = new Element("WORK_CODE");
			i_rps.addContent(i_rps_work_code);
			Element i_rps_art_code = new Element ("ART_CODE");
			i_rps.addContent(i_rps_art_code);
			Element i_rps_taker_ind = new Element("TAKER_IND");
			i_rps.addContent(i_rps_taker_ind);
			Element i_rps_docsub = new Element ("DOCSUB");
			i_rps.addContent(i_rps_docsub);
			Element i_rps_docori = new Element ("DOCORI");
			i_rps.addContent(i_rps_docori);
			Element i_rps_iss_ret = new Element ("ISS_RET");
			i_rps.addContent(i_rps_iss_ret);
			Element i_rps_taker_trans = new Element ("TRANS");
			i_rps.addContent(i_rps_taker_trans);
			Element i_rps_prov_name = new Element ("PROV_NAME");
			i_rps.addContent(i_rps_prov_name);
			Element i_rps_prov_addr_street = new Element ("PROV_ADDR_STREET");
			i_rps.addContent(i_rps_prov_addr_street);
			Element i_rps_prov_addr_num = new Element ("PROV_ADDR_NUM");
			i_rps.addContent(i_rps_prov_addr_num);
			Element i_rps_prov_addr_comp = new Element ("PROV_ADDR_COMP");
			i_rps.addContent(i_rps_prov_addr_comp);
			Element i_rps_prov_addr_dist = new Element ("PROV_ADDR_DIST");
			i_rps.addContent(i_rps_prov_addr_dist);
			Element i_rps_prov_city = new Element ("PROV_CITY");
			i_rps.addContent(i_rps_prov_city);
			Element i_rps_prov_uf = new Element ("PROV_UF");
			i_rps.addContent(i_rps_prov_uf);
			Element i_rps_prov_addr_post_code = new Element ("PROV_ADDR_POST_CODE");
			i_rps.addContent(i_rps_prov_addr_post_code);
			Element i_rps_prov_email = new Element ("PROV_EMAIL");
			i_rps.addContent(i_rps_prov_email);
			Element i_rps_flag_teste = new Element("FLAG_TESTE");
			i_rps.addContent(i_rps_flag_teste);
			
			Element i_xml_string = new Element("I_XML_STRING");
			header.addContent(i_xml_string);
			
			int countError = 0;
			int countAlerta = 0;
			
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(in);
			Element root = doc.getRootElement();
			List elements = root.getChildren();
			Iterator iter = elements.iterator();
			while (iter.hasNext()) {
				Element elem = (Element) iter.next();
			
				if("Alerta".equals(elem.getName())){
					
					String codigoIn = elem.getChildText("Codigo");
					String descricaoIn = elem.getChildText("Descricao");
					
					
					if(countAlerta <1){
						error_code_alerta.setText(codigoIn);	
						message_alerta.setText(descricaoIn);
					}
					
					if(countAlerta >0){
						Element it_alerta_local = new Element("IT_ALERTA");
						header.addContent(it_alerta_local);
						Element item_alerta_local = new Element("item");
						it_alerta_local.addContent(item_alerta_local);
						Element error_code_alerta_local = new Element("ERROR_CODE");
						item_alerta_local.addContent(error_code_alerta_local);
						Element message_alerta_local = new Element("MESSAGE");
						item_alerta_local.addContent(message_alerta_local);
						Element correction_alerta_local = new Element("CORRECTION");
						item_alerta_local.addContent(correction_alerta_local);	
						error_code_alerta_local.setText(codigoIn);
						message_alerta_local.setText(descricaoIn);
						
					}
					
					List listAlertaChilds = elem.getChildren();
					Iterator alertaChildIter = listAlertaChilds.iterator();
					while(alertaChildIter.hasNext()){
						
						
						Element alertaChildElement = (Element)alertaChildIter.next();
						
						/*if("Codigo".equals(alertaChildElement.getName())){
							String codigoIn = elem.getChildText("Codigo");
							error_code_alerta.setText(codigoIn);
							continue;
						}	
						if("Descricao".equals(alertaChildElement.getName())){
							String descricaoIn = elem.getChildText("Descricao");
							message_alerta.setText(descricaoIn);
							continue;
						}*/	
						if("ChaveRPS".equals(alertaChildElement.getName())){
							String inscPrestRPSIn = alertaChildElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestRPSIn);							
							
							String serieRPSIn = alertaChildElement.getChildText("SerieRPS");
							i_rps_series.setText(serieRPSIn);
							
							String numRPSIn = alertaChildElement.getChildText("NumeroRPS");
							rpsnum.setText(numRPSIn);
							continue;
						
						
							//Continuar mapeamento
						}
						if("ChaveNFe".equals(alertaChildElement.getName())){
							String inscPrestNfeIn = alertaChildElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestNfeIn);
							
							String numNfeIn = alertaChildElement.getChildText("NumeroNFe");
							nfsenum.setText(numNfeIn);
							
							String codVerifNfeIn = alertaChildElement.getChildText("CodigoVerificacao");
							vercode.setText(codVerifNfeIn);
							continue;
						}
					}
					countAlerta++;
					
				}
				if("Erro".equals(elem.getName())){
					
					String codigoIn = elem.getChildText("Codigo");
					String descricaoIn = elem.getChildText("Descricao");
					
					if(countError < 1){
						error_code.setText(codigoIn);
						message_error.setText(descricaoIn);
					}
					
					if(countError > 0){
						Element item_error_local = new Element("item");
						it_error.addContent(item_error_local);
						Element error_code_local = new Element("ERROR_CODE");
						item_error_local.addContent(error_code_local);
						error_code_local.setText(codigoIn);
						Element message_error_local = new Element("MESSAGE");
						item_error_local.addContent(message_error_local);
						message_error_local.setText(descricaoIn);
						Element correction_error_local = new Element("CORRECTION");
						item_error_local.addContent(correction_error_local);
						
					}
					
					List listErroChilds = elem.getChildren();
					Iterator erroChildIter = listErroChilds.iterator();
					while(erroChildIter.hasNext()){
						Element erroChildElement = (Element)erroChildIter.next();
						
					/*	if("Codigo".equals(erroChildElement.getName())){
							String codigoIn = elem.getChildText("Codigo");
							error_code.setText(codigoIn);
							continue;
						}	
						if("Descricao".equals(erroChildElement.getName())){
							String descricaoIn = elem.getChildText("Descricao");
							message_error.setText(descricaoIn);
							continue;
						}*/	
						if("ChaveRPS".equals(erroChildElement.getName())){
							
							String inscPrestRPSIn = erroChildElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestRPSIn);
							
							String serieRPSIn = erroChildElement.getChildText("SerieRPS");
							i_rps_series.setText(serieRPSIn);
							
							String numRPSIn = erroChildElement.getChildText("NumeroRPS");
							rpsnum.setText(numRPSIn);
							continue;
							
						}
						if("ChaveNFe".equals(erroChildElement.getName())){
							String inscPrestNfeIn = erroChildElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestNfeIn);
							
							String numNfeIn = erroChildElement.getChildText("NumeroNFe");
							nfsenum.setText(numNfeIn);
							
							String codVerifNfeIn = erroChildElement.getChildText("CodigoVerificacao");
							vercode.setText(codVerifNfeIn);	
							continue;
						}
					}
					countError++;
					
				}				

				if("NFe".equals(elem.getName())){
					Element nfe = new Element("NFe");
					header.addContent(nfe);
					
					List listNfesChilds = elem.getChildren();
					Iterator childNfeIter = listNfesChilds.iterator();
					
					while(childNfeIter.hasNext()){
						
						Element childNfeElement = (Element)childNfeIter.next();
						
						if("Assinatura".equals(childNfeElement.getName())){
							String assinaturaIn = elem.getChildText("Assinatura");
							signature.setText(assinaturaIn);
							continue;
							
						}
						
						if("ChaveNFe".equals(childNfeElement.getName())){
							String inscPrestNfeIn = childNfeElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestNfeIn);
							
							String numNfeIn = childNfeElement.getChildText("NumeroNFe");
							nfsenum.setText(numNfeIn);
							
							String codVerifNfeIn = childNfeElement.getChildText("CodigoVerificacao");
							vercode.setText(codVerifNfeIn);
							i_nfse_vercod.setText(codVerifNfeIn);
							continue;
						}
						
						if("DataEmissaoNFe".equals(childNfeElement.getName())){
							String dtEmissaoNfeIn = elem.getChildText("DataEmissaoNFe");
							issdate.setText(dtEmissaoNfeIn);
							i_nfse_issue_date.setText(dtEmissaoNfeIn);
							continue;
						}
						
						if("NumeroLote".equals(childNfeElement.getName())){
							String numLoteIn = elem.getChildText("NumeroLote");
							shipnum.setText(numLoteIn);
							continue;
						}
						
						if("ChaveRPS".equals(childNfeElement.getName())){
							String inscPrestRPSIn = childNfeElement.getChildText("InscricaoPrestador");
							i_rps_prov_im.setText(inscPrestRPSIn);	
							
							String serieRPSIn = childNfeElement.getChildText("SerieRPS");
							i_rps_series.setText(serieRPSIn);
							
							String numRPSIn = childNfeElement.getChildText("NumeroRPS");
							rpsnum.setText(numRPSIn);
							continue;
							
						}
						
						if("TipoRPS".equals(childNfeElement.getName())){
							String tipoRPSIn = elem.getChildText("TipoRPS");
							i_rps_type.setText(tipoRPSIn);	
							continue;
						
						}
						if("DataEmissaoRPS".equals(childNfeElement.getName())){
							String dtEmissaoRPSIn = elem.getChildText("DataEmissaoRPS");
							i_rps_issue_date.setText(dtEmissaoRPSIn);
							continue;
						}
						if("CPFCNPJPrestador".equals(childNfeElement.getName())){
							String cnpjIn = childNfeElement.getChildText("CNPJ");
							i_rps_prov_cnpj.setText(cnpjIn);
							continue;
						}
						
						if("RazaoSocialPrestador".equals(childNfeElement.getName())){
							String razaoSocialPrestadorIn = elem.getChildText("RazaoSocialPrestador");
							i_rps_prov_name.setText(razaoSocialPrestadorIn);
							continue;
							
						}
						
						if("EnderecoPrestador".equals(childNfeElement.getName())){
							String tipoLogradouroIn = childNfeElement.getChildText("TipoLogradouro");
							
							String logradouroIn = childNfeElement.getChildText("Logradouro");
							i_rps_prov_addr_street.setText(logradouroIn);
							
							String numeroEnderecoIn = childNfeElement.getChildText("NumeroEndereco");
							i_rps_prov_addr_num.setText(numeroEnderecoIn);
							
							String complementoEnderecoIn = childNfeElement.getChildText("ComplementoEndereco");
							i_rps_prov_addr_comp.setText(complementoEnderecoIn);
							
							String bairroIn = childNfeElement.getChildText("Bairro");
							i_rps_prov_addr_dist.setText(bairroIn);
							
							String cidadeIn = childNfeElement.getChildText("Cidade");
							i_rps_prov_city.setText(cidadeIn);
							
							String ufIn = childNfeElement.getChildText("UF");
							i_rps_prov_uf.setText(ufIn);
							
							String cepIn = childNfeElement.getChildText("CEP");
							i_rps_prov_addr_post_code.setText(cepIn);
						}
						
						if("EmailPrestador".equals(childNfeElement.getName())){
							String emailPrestadorIn = elem.getChildText("EmailPrestador");
							i_rps_prov_email.setText(emailPrestadorIn);
							continue;
						}
						
						
						if("StatusNFe".equals(childNfeElement.getName())){
							String statusNFeIn = elem.getChildText("StatusNFe");
							stat_in.setText(statusNFeIn);
							if("C".equals(statusNFeIn)){
								i_candate.setText(formatarDataHora());
							}
							continue;
							
						}
						
						if("DataCancelamento".equals(childNfeElement.getName())){
							String dataCancelamentoIn = elem.getChildText("DataCancelamento");
							candate.setText(dataCancelamentoIn);
							continue;
						}
						
						if("TributacaoNFe".equals(childNfeElement.getName())){
							String tributacaoNFeIn = elem.getChildText("TributacaoNFe");
							i_rps_nat_oper.setText(tributacaoNFeIn);
							continue;
							
						}
						if("OpcaoSimples".equals(childNfeElement.getName())){
							String opcaoSimplesIn = elem.getChildText("OpcaoSimples");
							opsimples.setText(opcaoSimplesIn);
							continue;
						}
						
						if("NumeroGuia".equals(childNfeElement.getName())){
							String numeroGuiaIn = elem.getChildText("NumeroGuia");
							numguia.setText(numeroGuiaIn);
							continue;
						}
						
						if("DataQuitacaoGuia".equals(childNfeElement.getName())){
							String dataQuitacaoGuiaIn = elem.getChildText("DataQuitacaoGuia");
							dtquitguia.setText(dataQuitacaoGuiaIn);
							continue;
						}
						
						if("ValorServicos".equals(childNfeElement.getName())){
							String valorServicosIn = elem.getChildText("ValorServicos");
							i_rps_amnt_serv.setText(valorServicosIn);
							continue;
						}
						if("ValorDeducoes".equals(childNfeElement.getName())){
							String valorDeducoesIn = elem.getChildText("ValorDeducoes");
							i_rps_amnt_dedu.setText(valorDeducoesIn);
							continue;
						}
						if("ValorPIS".equals(childNfeElement.getName())){
							String valorPISIn = elem.getChildText("ValorPIS");
							i_rps_amnt_pis.setText(valorPISIn);
							continue;
						}
						if("ValorCOFINS".equals(childNfeElement.getName())){
							String valorCOFINSIn = elem.getChildText("ValorCOFINS");
							i_rps_amnt_cof.setText(valorCOFINSIn);
							continue;
						}
						if("ValorINSS".equals(childNfeElement.getName())){
							String valorINSSIn = elem.getChildText("ValorINSS");
							i_rps_amnt_inss.setText(valorINSSIn);
							continue;
						}
						if("ValorIR".equals(childNfeElement.getName())){
							String valorIRIn = elem.getChildText("ValorIR");
							i_rps_amnt_ir.setText(valorIRIn);
							continue;
						}
						if("ValorCSLL".equals(childNfeElement.getName())){
							String valorCSLLIn = elem.getChildText("ValorCSLL");
							i_rps_amnt_csll.setText(valorCSLLIn);
							continue;
						}
						
						if("CodigoServico".equals(childNfeElement.getName())){
							String codigoServicoIn = elem.getChildText("CodigoServico");
							i_rps_serv_item.setText(codigoServicoIn);
							continue;
						}
						
						if("AliquotaServicos".equals(childNfeElement.getName())){
							String aliquotaServicosIn = elem.getChildText("AliquotaServicos");
							i_rps_aliquot.setText(aliquotaServicosIn);
							continue;
						}
						if("ValorISS".equals(childNfeElement.getName())){
							String valorISSIn = elem.getChildText("ValorISS");
							i_rps_amnt_iss.setText(valorISSIn);
							continue;
						}
						if("ValorCredito".equals(childNfeElement.getName())){
							String valorCreditoIn = elem.getChildText("ValorCredito");
							amnt_credito.setText(valorCreditoIn);
							continue;
						}
						if("ISSRetido".equals(childNfeElement.getName())){
							String iSSRetidoIn = elem.getChildText("ISSRetido");
							i_rps_iss_ret.setText(iSSRetidoIn);
							continue;
						}
						if("CPFCNPJTomador".equals(childNfeElement.getName())){
							String cnpjTomadorIn = childNfeElement.getChildText("CNPJ");
							i_rps_taker_cnpj.setText(cnpjTomadorIn);
							
							String cpfTomadorIn = childNfeElement.getChildText("CPF");
							i_rps_taker_cpf.setText(cpfTomadorIn);
							continue;
						}
						if("RazaoSocialTomador".equals(childNfeElement.getName())){
							String razaoSocialTomadorIn = elem.getChildText("RazaoSocialTomador");
							i_rps_taker_name.setText(razaoSocialTomadorIn);
							continue;
						}
						if("EnderecoTomador".equals(childNfeElement.getName())){
							
							String tipoLogradouroIn = childNfeElement.getChildText("TipoLogradouro");
							
							String logradouroIn = childNfeElement.getChildText("Logradouro");
							i_rps_taker_addr_street.setText(logradouroIn);
							
							String numeroEnderecoIn = childNfeElement.getChildText("NumeroEndereco");
							i_rps_taker_addr_num.setText(numeroEnderecoIn);
							
							String complementoEnderecoIn = childNfeElement.getChildText("ComplementoEndereco");
							i_rps_taker_addr_comp.setText(complementoEnderecoIn);
							
							String bairroIn = childNfeElement.getChildText("Bairro");
							i_rps_taker_addr_dist.setText(bairroIn);
							
							String cidadeIn = childNfeElement.getChildText("Cidade");
							i_rps_taker_city.setText(cidadeIn);
							
							String ufIn = childNfeElement.getChildText("UF");
							i_rps_taker_uf.setText(ufIn);
							
							String cepIn = childNfeElement.getChildText("CEP");
							i_rps_taker_addr_post_code.setText(cepIn);
							continue;
						}
						if("EmailTomador".equals(childNfeElement.getName())){
							String emailTomadorIn = elem.getChildText("EmailTomador");
							i_rps_taker_email.setText(emailTomadorIn);
							continue;
						}
						if("Discriminacao".equals(childNfeElement.getName())){
							String discriminacaoIn = elem.getChildText("Discriminacao");
							i_rps_text.setText(discriminacaoIn);
							continue;
						}
						
						
					}
					
				
				}
				
			
			}	
			
			Document jdoc = new Document(header);
			
			DOMOutputter outputter = new DOMOutputter();
			org.w3c.dom.Document docNative = outputter.output(jdoc);
			
			TransformerFactory tf=TransformerFactory.newInstance();
			Transformer transform=tf.newTransformer();
			
			transform.transform(new DOMSource(docNative), new StreamResult(out));
			
			XMLOutputter output = new XMLOutputter();
			System.out.println(output.outputString(jdoc));
			System.out.println();
			
		
		
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void setParameter(Map arg0) {
		// TODO Auto-generated method stub
		
	}
	
	private String formatarDataHora(){
		String formatoData = "yyyy-MM-dd";
		String formatoHora = "HH:mm:ss";
		SimpleDateFormat dfDate = new SimpleDateFormat(formatoData);
		SimpleDateFormat dfHora= new SimpleDateFormat(formatoHora);
		String dataAtual = dfDate.format(new Date());
		String horaAtual = dfHora.format(new Date());
		
		return dataAtual+"T"+horaAtual;
		
	}

}
